package cshi.common;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

/**
 * Created by shichen on 2017/12/7.
 */

class Hello implements HelloMBean {
    @Override
    public String getName() {
        return "cs";
    }

    @Override
    public void setName(String name) {

    }

    @Override
    public void print(String info) {
        System.out.println("hi " + info);
    }

    @Override
    public String getTelephone() {
        return "188";
    }
}

public class MBeanTest {
    public static void main(String[] args) throws  Exception{
        //create meabn server
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        //create object name
        //domain部分不一定是包名，冒号后是key，也必须写
        ObjectName helloName = new ObjectName("cshi.common2:type=hello2");
        //register mbean
        server.registerMBean(new Hello(),helloName);

        //调用helloName这个object的print方式，参数是后面的那些
        server.invoke(helloName,"print",new Object[]{"cs"}, new String[]{"java.lang.String"});

        Thread.sleep(Long.MAX_VALUE);
    }
}
